package com.sgb.kjwl.view.ui.activity.self.publish;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;


import com.swgk.core.util.AttachmentStore;
import com.swgk.core.util.FileUtil;
import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfTeamAddProjectBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.self.AddProjectEntity;
import com.sgb.kjwl.model.entity.self.ProjectTarRateEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.FileUtils;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.sgb.kjwl.viewmodel.self.publish.AddProjectModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
//新增工程
public class AddProjectActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {

    public static final int REQUEST_CODE_FILE_CHOOSE = 102;

    @Inject
    AddProjectModel viewModel;
    private ActivitySelfTeamAddProjectBinding addProjectBinding;

    private Thread thread;
    private static final int MSG_LOAD_DATA = 0x0001;
    private static final int MSG_LOAD_SUCCESS = 0x0002;

    private List<PickSelectEntity> teamSizes;
    private List<PickSelectEntity> teamTypes;
    private List<ProjectTarRateEntity> tarRateSize;

    // 一级联动数据
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    private List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    private List<List<List<AddressEntity>>> opt3 = new ArrayList<>();

    private boolean isLoaded = false;
    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;

    //日期
    private Calendar selectedDate;
    private Date today;

    public static void start(AppCompatActivity context, int code) {
        Intent intent = new Intent();
        intent.setClass(context, AddProjectActivity.class);
        context.startActivityForResult(intent, code);
    }

    public static void start(AppCompatActivity context, String id, int code) {
        Intent intent = new Intent();
        intent.setClass(context, AddProjectActivity.class);
        intent.putExtra(AppConstant.EXTRA_ENTITY, id);
        context.startActivityForResult(intent, code);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        String id = getIntent().getStringExtra(AppConstant.EXTRA_ENTITY);
        addProjectBinding = DataBindingUtil.setContentView(this, R.layout.activity_self_team_add_project);
        addProjectBinding.setViewModel(viewModel.addProjectEntity.get());
        addProjectBinding.include.ivBack.setOnClickListener(this);
        addProjectBinding.nextView.setOnClickListener(this);
        addProjectBinding.itemTvRange.setOnWholeItemClickListener(this);
        addProjectBinding.itemTvSort.setOnWholeItemClickListener(this);
        addProjectBinding.itemTvStartTime.setOnWholeItemClickListener(this);
        addProjectBinding.itemTvOverTime.setOnWholeItemClickListener(this);
        addProjectBinding.itemTvTeamSize.setOnWholeItemClickListener(this);
        addProjectBinding.itemTvTeamTime.setOnWholeItemClickListener(this);
        addProjectBinding.itemTvEndTime.setOnWholeItemClickListener(this);
        addProjectBinding.itemTvTeamType.setOnWholeItemClickListener(this);
        addProjectBinding.itemTvTaxRate.setOnWholeItemClickListener(this);
        addProjectBinding.tvUploadFile.setOnClickListener(this);
        addProjectBinding.tvFileDelete.setOnClickListener(this);
        addProjectBinding.itemTvName.getContent_edt().setInputType(InputType.TYPE_CLASS_TEXT);
        addProjectBinding.itemTvPayType.getContent_edt().setInputType(InputType.TYPE_CLASS_TEXT);
        addProjectBinding.itemTvBelongProject.getContent_edt().setInputType(InputType.TYPE_CLASS_TEXT);
        addProjectBinding.itemTvCostProject.getContent_edt().setInputType(InputType.TYPE_CLASS_NUMBER);
        addProjectBinding.itemTvTotalPackage.getContent_edt().setInputType(InputType.TYPE_CLASS_TEXT);
        addProjectBinding.itemTvPerson.getContent_edt().setInputType(InputType.TYPE_CLASS_TEXT);
        addProjectBinding.itemTvPhone.getContent_edt().setInputType(InputType.TYPE_CLASS_PHONE);
        addProjectBinding.itemTvWarranty.getContent_edt().setInputType(InputType.TYPE_CLASS_NUMBER);
        addProjectBinding.radioGroup.setOnCheckedChangeListener(new RadioGroupListener());

        addProjectBinding.fileLayout.setVisibility(View.GONE);

        if (TextUtils.isEmpty(id)) {
            addProjectBinding.include.titleView.setText("新增工程");
            getEntity().getRequirementDTO().setHasTax(String.valueOf(1));
        } else {
            addProjectBinding.include.titleView.setText("编辑工程");
            addProjectBinding.pushView.setText("编辑");
            viewModel.detailProject(id);
        }
    }

    public void displayData(AddProjectEntity data) {
        if (null != data) {
            viewModel.addProjectEntity.set(data);
            addProjectBinding.setViewModel(data);
            showWorkType();
            String address = data.getEngineeringInfoDTO().getProvinceName() + data.getEngineeringInfoDTO().getCityName()
                    + data.getEngineeringInfoDTO().getAreaName();
            addProjectBinding.itemTvRange.getContent_edt().setText(address);
            addProjectBinding.itemTvStartTime.getContent_edt().setText(data.getEngineeringInfoDTO().getStartTimeStr());
            addProjectBinding.itemTvOverTime.getContent_edt().setText(data.getEngineeringInfoDTO().getEndTimeStr());
            addProjectBinding.itemTvEndTime.getContent_edt().setText(data.getEngineeringInfoDTO().getEndDateStr());
            addProjectBinding.itemTvTeamTime.getContent_edt().setText(data.getRequirementDTO().getAdmissionTimeStr());
            addProjectBinding.itemTvTaxRate.getContent_edt().setText(data.getRequirementDTO().getRate() + "%");
            String teamSize = data.getRequirementDTO().getMinSize() + "-" + data.getRequirementDTO().getMaxSize() + "人";
            addProjectBinding.itemTvTeamSize.getContent_edt().setText(teamSize);
            if (data.getRequirementDTO().getIsCompany().equals("1")) {
                addProjectBinding.itemTvTeamType.getContent_edt().setText("企业");
            } else {
                addProjectBinding.itemTvTeamType.getContent_edt().setText("个人");
            }
            if (data.getRequirementDTO().getHasTax().equals("1")) {
                addProjectBinding.ysBtn.setChecked(true);
            } else {
                addProjectBinding.noBtn.setChecked(true);
            }
            String filePath = data.getEngineeringInfoDTO().getFilePath();
            addProjectBinding.fileLayout.setVisibility(TextUtils.isEmpty(filePath) ? View.GONE : View.VISIBLE);
            if (!TextUtils.isEmpty(filePath)) {
                addProjectBinding.tvFileName.setText(FileUtil.getFileNameFromPath(filePath));
            }
        }
    }

    @Override
    protected void init() {
        selectedDate = Calendar.getInstance();
        viewModel.getLocationLL();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.next_view:
                if (isInputValid()) {
                    addProjectBinding.tS.setVisibility(View.VISIBLE);
                    addProjectBinding.nS.setVisibility(View.GONE);
                    addProjectBinding.include.titleView.setText("团队要求");
                    addProjectBinding.include.rightView.setText("上一步");
                    addProjectBinding.include.rightView.setOnClickListener(this);
                    addProjectBinding.pushView.setOnClickListener(this);
                }
                break;
            case R.id.right_view:
                addProjectBinding.tS.setVisibility(View.GONE);
                addProjectBinding.nS.setVisibility(View.VISIBLE);
                addProjectBinding.include.titleView.setText("新增工程");
                addProjectBinding.include.rightView.setText("");
                addProjectBinding.include.rightView.setClickable(false);
                break;
            case R.id.push_view:
                if (isInputValid()) {
                    if (isInputValidTeam()) {
                        String filePath = getEntity().getEngineeringInfoDTO().getFilePath();
                        if (!TextUtils.isEmpty(filePath) && !filePath.startsWith("http")) {
                            viewModel.uploadSingleImage(new File(filePath));
                            return;
                        }
                        viewModel.addProjectModel();
                    }
                }
                break;
            case R.id.tv_upload_file:
                browseDocuments();
                break;
            case R.id.tv_file_delete:
                addProjectBinding.tvFileName.setText("");
                addProjectBinding.fileLayout.setVisibility(View.GONE);
                getEntity().getEngineeringInfoDTO().setFilePath("");
                break;
        }
    }

    // 上传文件成功回调
    public void uploadImageSuccess(String toString) {
        getEntity().getEngineeringInfoDTO().setFilePath(toString);
        viewModel.addProjectModel();
    }

    private void browseDocuments() {
        String[] mimeTypes =
                {"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
                        "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
                        "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
                        "text/plain",
                        "application/pdf",
                        "application/zip"};
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            intent.setType("*/*");
            intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        } else {
            StringBuilder mimeTypesStr = new StringBuilder();
            for (String mimeType : mimeTypes) {
                mimeTypesStr.append(mimeType).append("|");
            }
            String s = mimeTypesStr.toString();
            intent.setType(s.substring(0, s.length() - 1));
        }
        startActivityForResult(Intent.createChooser(intent, "ChooseFile"), REQUEST_CODE_FILE_CHOOSE);
    }

    /**
     * 检查文件大小
     *
     * @param filePath 文件路径
     * @return 文件大小
     */
    private boolean checkFileSize(String filePath) {
        float sizeCount = 0f;
        long fileLength = AttachmentStore.getFileLength(filePath);
        String fileSize = FileUtil.formatFileSize(fileLength, FileUtil.SizeUnit.MB);
        if (fileSize.contains("MB")) {
            String fileSizeCount = fileSize.replace("MB", "");
            if (!TextUtils.isEmpty(fileSizeCount)) {
                sizeCount = Float.parseFloat(fileSizeCount);
                if (sizeCount > 10) {
                    MToast.showToast(this, "可上传不超过10M的文件");
                    return false;
                }
            }
        }
        LogUtil.d("Size", "checkFileSize :" + fileSize);
        return true;
    }

    private boolean isInputValid() {
        if (TextUtils.isEmpty(addProjectBinding.itemTvName.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请输入工程名称");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvSort.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请选择专业分类");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvContractCompany.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请输入建设单位");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvContractCompany.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请输入建设单位");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvCostProject.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请输入工程总造价");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.etDetailAddress.getEditContent())) {
            MToast.showToast(AddProjectActivity.this, "请输入详细地址");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvEndTime.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请选择截止时间");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvStartTime.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请选择开始时间");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvOverTime.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请选择结束时间");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvPerson.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请输入联系人姓名");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvPhone.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请输入联系人电话");
            return false;
        } else {
            if (!CheckUtils.isMobile(addProjectBinding.itemTvPhone.getContent().trim())) {
                Toast.makeText(this, "请填写正确的电话", Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }

    private boolean isInputValidTeam() {
        if (TextUtils.isEmpty(addProjectBinding.itemTvTeamSize.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请选择团队规模");
            return false;
        } else if (TextUtils.isEmpty(addProjectBinding.itemTvTeamTime.getContent())) {
            MToast.showToast(AddProjectActivity.this, "请选择到场时间");
            return false;
        } else if (getEntity().getRequirementDTO().getHasTax().equals("1") && TextUtils.isEmpty(getEntity().getRequirementDTO().getTaxRate())) {
            MToast.showToast(AddProjectActivity.this, "请选择税率");
            return false;
        }
        return true;
    }

    public AddProjectEntity getEntity() {
        return viewModel.addProjectEntity.get();
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.item_tv_range:
                if (addressEntityList != null) {
                    showCityPop();
                } else {
                    viewModel.getAddressModel();
                }
                break;
            case R.id.item_tv_sort:
                viewModel.showPop(view);
                break;
            case R.id.item_tv_start_time:
                showTimePop(1);
                break;
            case R.id.item_tv_over_time:
                if (startTime > 0) {
                    showTimePop(2);
                } else {
                    MToast.showToast(AddProjectActivity.this, "请先选择开始时间");
                }
                break;
            case R.id.item_tv_team_size:
                viewModel.getGm();
                break;
            case R.id.item_tv_team_time:
                showTimePop(3);
                break;
            case R.id.item_tv_end_time:
                showTimePop(4);
                break;
            case R.id.item_tv_team_type:
                viewModel.getLx();
                break;
            case R.id.item_tv_tax_rate:
                viewModel.getProjectTarRate();
                break;
        }
    }

    //所在地区选择
    public void showAddressPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        this.addressEntityList = datas;
        if (isLoaded) {
            showCityPop();
        } else {
            mHandler.sendEmptyMessage(MSG_LOAD_DATA);
        }
    }

    @SuppressWarnings("unchecked")
    public void showCityPop() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(AddProjectActivity.this, (options1, options2, options3, v) -> {
            String areaName;
            if (opt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
            } else {
                areaName = opt3.get(options1).get(options2).get(options3).getName();
                cityPosition3 = options3;
                getEntity().getEngineeringInfoDTO().setAreaNo(opt3.get(options1).get(options2).get(options3).getNo());
                getEntity().getEngineeringInfoDTO().setAreaName(areaName);
            }
            String content = addressEntityList.get(options1).getName()
                    + opt2.get(options1).get(options2).getName()
                    + areaName;
            addProjectBinding.itemTvRange.setContent(content);
            cityPosition1 = options1;
            cityPosition2 = options2;
            getEntity().getEngineeringInfoDTO().setProvinceName(addressEntityList.get(options1).getName());
            getEntity().getEngineeringInfoDTO().setProvinceNo(addressEntityList.get(options1).getId());
            getEntity().getEngineeringInfoDTO().setCityName(opt2.get(options1).get(options2).getName());
            getEntity().getEngineeringInfoDTO().setCityNo(opt2.get(options1).get(options2).getNo());
        }).build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_LOAD_DATA:
                    if (thread == null) {//如果已创建就不再重新创建子线程了
                        thread = new Thread(() -> {
                            // 子线程中解析省市区数据
                            initJsonData();
                        });
                        thread.start();
                    }
                    break;
                case MSG_LOAD_SUCCESS:
                    showCityPop();
                    isLoaded = true;
                    thread = null;
                    break;
            }
        }
    };

    private void initJsonData() {
        //城市列表
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") List<AddressEntity<List<AddressEntity>>> cityList;
        //区域列表
        List<List<AddressEntity>> areaList;
        for (int x = 0; x < addressEntityList.size(); x++) {
            cityList = new ArrayList<>();
            areaList = new ArrayList<>();
            if (getEntity() != null && getEntity().getEngineeringInfoDTO().getProvinceName() != null && getEntity().getEngineeringInfoDTO().getProvinceName().equals(addressEntityList.get(x).getName())) {
                cityPosition1 = x;
            }
            opt2.add(addressEntityList.get(x).getChildren());
            for (int i = 0; i < addressEntityList.get(x).getChildren().size(); i++) {
                cityList.add(addressEntityList.get(x).getChildren().get(i));
                List<AddressEntity> cityAreaList = new ArrayList<>();
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (addressEntityList.get(x).getChildren().get(i).getChildren() == null
                        || addressEntityList.get(x).getChildren().get(i).getChildren().size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(addressEntityList.get(x).getChildren().get(i).getChildren());
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
        if (getEntity() != null) {
            if (getEntity().getEngineeringInfoDTO().getCityName() != null)
                for (int x2 = 0; x2 < opt2.size(); x2++) {
                    if (getEntity().getEngineeringInfoDTO().getCityName().equals(addressEntityList.get(cityPosition1).getChildren().get(x2).getName())) {
                        cityPosition2 = x2;
                        break;
                    }
                }
            if (getEntity().getEngineeringInfoDTO().getAreaName() != null)
                for (int x3 = 0; x3 < opt3.size(); x3++) {
                    if (getEntity().getEngineeringInfoDTO().getAreaName().equals(addressEntityList.get(cityPosition1).getChildren().get(cityPosition2).getChildren().get(x3).getName())) {
                        cityPosition3 = x3;
                        break;
                    }
                }
        }
        mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);
    }

    private long startTime = 0;

    //时间选择器
    public void showTimePop(int flag) {
        TimePickerBuilder timePickerBuilder = new TimePickerBuilder(AddProjectActivity.this, (date, v) -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            if (today == null)
                today = new Date();
            int compareTo = today.compareTo(date);

            try {
                if (flag == 1) {
                    selectedDate.setTime(date);
                    //时间戳
                    long times = DateUtils.dateTo13Timestamp(format);
                    //MLog.d(TAG, "releaseGoods shipTime is:" + times);
                    startTime = times;
                    addProjectBinding.itemTvStartTime.setContent(format);
                    getEntity().getEngineeringInfoDTO().setStartTime(String.valueOf(times));
                }
                if (flag == 2) {
                    selectedDate.setTime(date);
                    //时间戳
                    long times = DateUtils.dateTo13Timestamp(format);
                    //MLog.d(TAG, "releaseGoods shipTime is:" + times);
                    long ls = DateUtils.dateTo10Timestamp(DateUtils.timestampToDate(times));
                    long le = DateUtils.dateTo10Timestamp(DateUtils.timestampToDate(startTime));
                    if (ls <= le) {
                        MToast.showToast(AddProjectActivity.this, "请选择大于开始时间的日期");
                    } else {
                        addProjectBinding.itemTvOverTime.setContent(format);
                        getEntity().getEngineeringInfoDTO().setEndTime(String.valueOf(times));
                    }
                }
                if (flag == 3) {
                    addProjectBinding.itemTvTeamTime.setContent(format);
                    selectedDate.setTime(date);
                    //时间戳
                    long times = DateUtils.dateTo13Timestamp(format);
                    //MLog.d(TAG, "releaseGoods shipTime is:" + times);
                    getEntity().getRequirementDTO().setAdmissionTime(String.valueOf(times));
                }
                if (flag == 4) {
                    selectedDate.setTime(date);
                    //时间戳
                    long times = DateUtils.dateTo13Timestamp(format);
                    //MLog.d(TAG, "releaseGoods shipTime is:" + times);
//                    long ls = DateUtils.dateTo10Timestamp(DateUtils.timestampToDate(times));
//                    long le = DateUtils.dateTo10Timestamp(DateUtils.timestampToDate(startTime));
                    if (compareTo >= 0) {
                        MToast.showToast(AddProjectActivity.this, "请选择大于今日日期的时间");
                    } else {
                        addProjectBinding.itemTvEndTime.setContent(format);
                        getEntity().getEngineeringInfoDTO().setEndDate(String.valueOf(times));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).setType(new boolean[]{true, true, true, false, false, false});

        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        String[] split = format.split("-");
        startDate.set(Integer.parseInt(split[0]), Integer.parseInt(split[1]) - 1, Integer.parseInt(split[2]));//设置起始年份
        Calendar endDate = Calendar.getInstance();
        endDate.set(2099, 11, 31);//设置结束年份

        timePickerBuilder.setRangDate(startDate, endDate);
        TimePickerView timePicker = timePickerBuilder.build();
        timePicker.show();
        timePicker.setDate(selectedDate);
    }

    public void showWorkType() {
        StringBuilder sb = new StringBuilder();
        if (!TextUtils.isEmpty(getEntity().getEngineeringInfoDTO().getFirstCategoryName())) {
            sb.append(getEntity().getEngineeringInfoDTO().getFirstCategoryName());
        }
        if (!TextUtils.isEmpty(getEntity().getEngineeringInfoDTO().getSecondCategoryName())) {
            sb.append("-").append(getEntity().getEngineeringInfoDTO().getSecondCategoryName());
        }
        if (!TextUtils.isEmpty(getEntity().getEngineeringInfoDTO().getThirdCategoryName())) {
            sb.append("-").append(getEntity().getEngineeringInfoDTO().getThirdCategoryName());
        }
        if (!TextUtils.isEmpty(getEntity().getEngineeringInfoDTO().getFourCategoryName())) {
            sb.append("-").append(getEntity().getEngineeringInfoDTO().getFourCategoryName());
        }
        if (!TextUtils.isEmpty(getEntity().getEngineeringInfoDTO().getFifthCategoryName())) {
            sb.append("-").append(getEntity().getEngineeringInfoDTO().getFifthCategoryName());
        }
        if (!TextUtils.isEmpty(getEntity().getEngineeringInfoDTO().getSixthCategoryName())) {
            sb.append("-").append(getEntity().getEngineeringInfoDTO().getSixthCategoryName());
        }
        if (!TextUtils.isEmpty(sb.toString())) {
            addProjectBinding.itemTvSort.setContent(sb.toString());
        }
    }

    //规模
    public void setGmDatas(List<PickSelectEntity> data) {
        if (null == teamSizes) {
            teamSizes = new ArrayList<>();
        }
        teamSizes.clear();
        teamSizes.addAll(data);
        if (null != teamSizes && teamSizes.size() > 0) {
            OptionsPickerView businessOption = new OptionsPickerBuilder(AddProjectActivity.this, (options1, options2, options3, v) -> {
                String content = teamSizes.get(options1).getProjectscale();
                addProjectBinding.itemTvTeamSize.setContent(content);
                getEntity().getRequirementDTO().setTeamSize((String) teamSizes.get(options1).getNo());
            }).build();
            businessOption.setPicker(teamSizes);
            businessOption.setSelectOptions(0);
            businessOption.show();
        }
    }

    //税率
    public void setTarRate(List<ProjectTarRateEntity> data) {
        if (null == tarRateSize) {
            tarRateSize = new ArrayList<>();
        }
        tarRateSize.clear();
        tarRateSize.addAll(data);
        if (null != tarRateSize && tarRateSize.size() > 0) {
            OptionsPickerView businessOption = new OptionsPickerBuilder(AddProjectActivity.this, (options1, options2, options3, v) -> {
                String content = tarRateSize.get(options1).getRate();
                addProjectBinding.itemTvTaxRate.setContent(content + "%");
                getEntity().getRequirementDTO().setTaxRate((String) tarRateSize.get(options1).getNo());
            }).build();
            businessOption.setPicker(tarRateSize);
            businessOption.setSelectOptions(0);
            businessOption.show();
        }
    }

    public void setLxDatas(List<PickSelectEntity> list) {
        if (null == teamTypes) {
            teamTypes = new ArrayList<>();
        }
        teamTypes.clear();
        teamTypes.addAll(list);
        if (null != teamTypes && teamTypes.size() > 0) {
            OptionsPickerView businessOption = new OptionsPickerBuilder(AddProjectActivity.this, (options1, options2, options3, v) -> {
                String content = teamTypes.get(options1).getProjectscale();
                addProjectBinding.itemTvTeamType.setContent(content);
                getEntity().getRequirementDTO().setIsCompany(teamTypes.get(options1).getNo());
            }).build();
            businessOption.setPicker(teamTypes);
            businessOption.setSelectOptions(0);
            businessOption.show();
        }
    }

    class RadioGroupListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (group == addProjectBinding.radioGroup) {
                if (checkedId == addProjectBinding.ysBtn.getId()) {
                    getEntity().getRequirementDTO().setHasTax(String.valueOf(1));
                    addProjectBinding.itemTvTaxRate.setVisibility(View.VISIBLE);
                } else if (checkedId == addProjectBinding.noBtn.getId()) {
                    getEntity().getRequirementDTO().setHasTax(String.valueOf(0));
                    addProjectBinding.itemTvTaxRate.setVisibility(View.GONE);
                }
            }
        }
    }

    public void setLocationLL(MapLongAndLatEntity data) {
        getEntity().getEngineeringInfoDTO().setLatitude(data.getLat());
        getEntity().getEngineeringInfoDTO().setLongitude(data.getLng());
    }

    public void BeckProjectActivity() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_FILE_CHOOSE) {
                Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程。
                String realPathFromURI;
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {//4.4以后
                    realPathFromURI = FileUtils.getPath(this, uri);
                } else {//4.4以下下系统调用方法
                    realPathFromURI = FileUtils.getRealPathFromURI(uri, this);
                }
                if (checkFileSize(realPathFromURI)) {
                    getEntity().getEngineeringInfoDTO().setFilePath(realPathFromURI);
                    addProjectBinding.tvFileName.setText(FileUtil.getFileNameFromPath(realPathFromURI));
                    addProjectBinding.fileLayout.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}
